package tech.aistar.service;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.mapper.UserLoveMapper;
import tech.aistar.pojo.UserLove;
import tech.aistar.vo.ResultVo;
import java.util.List;
import java.util.concurrent.TimeUnit;



@Service
@Transactional
public class UserLoveServiceImpl implements UserLoveService {
    @Autowired
    private UserLoveMapper userLoveMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //我的喜欢
    @Override
    public ResultVo<UserLove> findByUid(Integer uid) {
        ResultVo<UserLove> resultVo = new ResultVo<>();
        resultVo.setCode(0);
        resultVo.setMsg("查询成功");
        resultVo.setCount(userLoveMapper.selectCount(uid));

        List<UserLove> userLoveList = userLoveMapper.findByUid(uid);
        for (UserLove userLove : userLoveList) {
            ResultVo<UserLove> resultVo1 = new ResultVo<>();
            BeanUtils.copyProperties(userLove, resultVo1);
        }
        resultVo.setData(userLoveList);

        return resultVo;
    }

    @Override
    public int insetBySingerName(UserLove userLove) {
        return userLoveMapper.insetBySingerName(userLove);
    }

    @Override
    public void delete() {
        userLoveMapper.delete();
    }
    //喜欢的判断
    @Override
    public UserLove findByUserAndSid(Integer uid, Integer sid) {
        return userLoveMapper.findByUserAndSid(uid,sid);
    }

    //点赞，要传入的是帖子id，用户id，用户点赞状态0代表取消,1代表点赞
    public void updateLike( Long userId,UserLove userLove, Integer likeStatus) {
        //单独记录帖子本身的键的设计
        String key = "UserId:" + userId;
        //记录用户点赞具体帖子的键值设计
        String userKey = "USERID_likeStatus:" +userId +"-" + likeStatus;
        //如果redis中有这个键
        if (redisTemplate.hasKey(key)) {
            //我们将相关的值取出来，这个值就是该帖子的点赞总数
            Object o = redisTemplate.opsForValue().get(key);
            if (o != null) {
                //取出点赞总数值，转换成int类型进行总数加减
                int likeNum = Integer.parseInt(o.toString());
                //如果用户点赞
                if (likeStatus == 1) {
                    try {
                        //逻辑1：帖子点赞总数+1，存储对应的键值，key为键，点赞后点赞总数加一存储
                        redisTemplate.opsForValue().set(key, userLove, 10L, TimeUnit.MINUTES);
                        //逻辑2：记录对用户的点赞情况，将用户和对应帖子绑定，在redis中存储对应的键
                        redisTemplate.opsForValue().set(userKey, userLove, 1L, TimeUnit.DAYS);
                    } catch (Exception e) {

                    }

                    // 写入数据库,其实也可以在数据库中记录对应点赞情况，这样是防止点赞redis过期，具体业务具体分析;
                    userLoveMapper.insetBySingerName(userLove);

                } else {
                    try {
                        redisTemplate.opsForValue().set(key, userLove, 10L, TimeUnit.MINUTES);
                        redisTemplate.delete(userKey);
                    } catch (Exception e) {

                    }
                   userLoveMapper.delete();

                }
            }
        }
    }



}